#include<iostream>
#include<string>
#include<cstring>

class Person
{
public:

    Person():name_(nullptr){std::cout << "Person default constructor." << std::endl;};
    Person(const char *name)
    {
        if (name)
        {
            name_ = new char(strlen(name) + 1);
            strcpy(name_,name);
        }
    };

    //深拷贝构造函数
    Person(const Person &person)
    {
        if (person.name_)
        {
            this->name_ = new char(strlen(person.name_) + 1);
            strcpy(this->name_,person.name_);
        }
    };

    //深拷贝赋值运算符
    Person& operator=(const Person &person)
    {
        if (person.name_)
        {
            this->name_ = new char(strlen(person.name_) + 1);
            strcpy(this->name_,person.name_);
        }
        return *this;
    }

    //打印名字
    void PrintName(){if(name_){std::cout << "name:" << name_ << std::endl;}};
    ~Person()
    {  
        delete[] name_;//释放内存
        std::cout << "Person destructor." << std::endl;
    };

    //需要动态申请的内存变量，但是没有自定义copy 构造函数和赋值运算符
private:
    char *name_;
};

int main(int argc, char const *argv[])
{
    Person person1("alang");
    person1.PrintName();
    Person person2;
    person2 = person1;
    person2.PrintName();
    return 0;
}
